cc.Class({
    extends: cc.Component,
	
	ctor: function (x,y,type1,type2,mapNode,blockNode,game,no) {
		this.x=x;
		this.y=y;
		
		this.type1=type1;
		this.type2=type2;
		
		this.mapNode=mapNode;
		this.iceNode=null;
		this.blockNode=blockNode;
		//this.blockNode.zIndex = 1;
		
		this.game=game;
		
		this.mpU=null;
		this.mpD=null;
		this.mpUL=null;
		this.mpUR=null;
		this.mpDL=null;
		this.mpDR=null;
		
		this.no = no;
		
		this.effectFlag=0;
		
		this.waitChangeType=1;
		
		this.newNmb=0;//记录顶层块的新增个数
		
		this.movePoints=[];//移动轨迹
		
		this.moveSpeed = 0.1;
		
		this.createFlag = false;
		
		this.toScoreCallback = cc.callFunc(function () {
				this.moveByPoints();
		}, this); 
		
	},
	
	moveByPoints(){
		
		if(this.movePoints.length>0){
			var point = this.movePoints[this.movePoints.length-1];
			this.movePoints.splice(this.movePoints.length-1,1);
			//this.movePoints.length--;
			this.blockNode.runAction(cc.sequence(cc.moveTo(this.moveSpeed,point.x,point.y),this.toScoreCallback));
		}else{
			
			this.blockNode.runAction(cc.sequence(cc.moveBy(0.1,0,-10),cc.moveBy(0.02,0,12),cc.moveBy(0.02,0,-2)));
			this.blockNode.runAction(cc.sequence(
			cc.scaleTo(0.1,this.game.blockScale,this.game.blockScale*0.8),
			cc.scaleTo(0.02,this.game.blockScale,this.game.blockScale*1.2),
			cc.scaleTo(0.02,this.game.blockScale,this.game.blockScale)));
		}
		
	},
	
	getGroup(){
		if(this.group){
			return this.group;
		}else{
			return [this.mpU,this.mpD,this.mpUL,this.mpUR,this.mpDL,this.mpDR];
		}
	},
	
	touched(tx,ty){
		var startx = this.x-this.game.w2/2,starty = this.y - this.game.h/2;
		var endx = this.x+this.game.w2/2,endy = this.y+this.game.h/2;
				
		if(tx>startx&&tx<endx&&ty<endy&&ty>starty){
			return true;
		}
		
		return false;
	},
	
	isNear(tmp){
		return tmp == this.mpU || tmp == this.mpD || tmp == this.mpUL || tmp == this.mpUR || tmp == this.mpDR || tmp == this.mpDL ;
	},
	
	
	//{type:1,arr:[[]]}
	findSame2(dir){
		
		
		if(this.type1>6)
			return {type:0};
		
		var result={};
		var total = [];
		var lineNmb = 0;
		var longlineNmb = 0;
		var longest = 0;
		var tdir=2;
		
		
		//U 
		var sameU = [];
		var node = this.mpU;
		while(node!=null&&node.type1==this.type1&&node.type2!=8){
			sameU.push(node);
			node = node.mpU;
		}
		
		//D
		var sameD = [];
		node = this.mpD;
		while(node!=null&&node.type1==this.type1&&node.type2!=8){
			sameD.push(node);
			node = node.mpD;
		}
		
		var udlength = sameU.length+sameD.length;
		if(udlength>=2){
			tdir=2;
			longest = udlength;
			if(sameU.length>0){
				lineNmb++;
				//total=total.concat(sameU);
				total.push(sameU);
			}
			if(sameD.length>0){
				lineNmb++;
				//total=total.concat(sameD);
				total.push(sameD);
			}
			longlineNmb++;
		}
		
		
		//UL
		var sameUL = [];
		var node = this.mpUL;
		while(node!=null&&node.type1==this.type1&&node.type2!=8){
			sameUL.push(node);
			node = node.mpUL;
		}
		
		//DR
		var sameDR = [];
		node = this.mpDR;
		while(node!=null&&node.type1==this.type1&&node.type2!=8){
			sameDR.push(node);
			node = node.mpDR;
		}
		
		var uldrlength = sameUL.length+sameDR.length;
		if(uldrlength>=2){
			
			tdir = uldrlength>longest?3:tdir;
			
			longest = uldrlength>longest?uldrlength:longest;
			
			if(sameUL.length>0){
				lineNmb++;
				//total=total.concat(sameUL);
				total.push(sameUL);
			}
			if(sameDR.length>0){
				lineNmb++;
				//total=total.concat(sameDR);
				total.push(sameDR);
			}
			longlineNmb++;
		}
		
		//UR
		var sameUR = [];
		var node = this.mpUR;
		while(node!=null&&node.type1==this.type1&&node.type2!=8){
			sameUR.push(node);
			node = node.mpUR;
		}
		
		//DL
		var sameDL = [];
		node = this.mpDL;
		while(node!=null&&node.type1==this.type1&&node.type2!=8){
			sameDL.push(node);
			node = node.mpDL;
		}
		
		var urdllength = sameUR.length+sameDL.length;
		if(urdllength>=2){
			
			tdir = urdllength>longest?4:tdir;
			
			longest = urdllength>longest?urdllength:longest;
			
			if(sameUR.length>0){
				lineNmb++;
				//total=total.concat(sameUR);
				total.push(sameUR);
			}
			if(sameDL.length>0){
				lineNmb++;
				//total=total.concat(sameDL);
				total.push(sameDL);
			}
			longlineNmb++;
		}
		
		result.arr = total;
		
		//console.log('longest',longest);
		
		if(longest>=4){//彩色
			result.type=8;
		}else if(lineNmb>=4){//五角星
			result.type=7;
		}else if(lineNmb==3){//花
			result.type=6;
		}else if(lineNmb==2&&longlineNmb>=2){//三角形
			result.type=5;
		}else if(longest==3){//竖线 * 3
			if(dir)
				result.type = dir;
			else
				result.type = tdir;
		}else if(longest==2){//普通
			total.push([this]);
			result.type=1;
		}else{//没有消除
			result.type=0;
		}
		
		
		return result;
		
	},
	
	findSame(){
		var total = [];
		var same1 = [this],same2=[this],same3=[this];
		
		//U D
		var node = this.mpU;
		while(node!=null&&node.type1==this.type1&&node.type2!=8){
			same1.push(node);
			node = node.mpU;
		}
		
		node = this.mpD;
		while(node!=null&&node.type1==this.type1&&node.type2!=8){
			same1.push(node);
			node = node.mpD;
		}
		
		if(same1.length>2){
			total.push(same1);
		}
		
		//UL DR
		var node = this.mpUL;
		while(node!=null&&node.type1==this.type1&&node.type2!=8){
			same2.push(node);
			node = node.mpUL;
		}
		
		node = this.mpDR;
		while(node!=null&&node.type1==this.type1&&node.type2!=8){
			same2.push(node);
			node = node.mpDR;
		}
		
		if(same2.length>2){
			total.push(same2);
		}
		
		//UR DL
		var node = this.mpUR;
		while(node!=null&&node.type1==this.type1&&node.type2!=8){
			same3.push(node);
			node = node.mpUR;
		}
		
		node = this.mpDL;
		while(node!=null&&node.type1==this.type1&&node.type2!=8){
			same3.push(node);
			node = node.mpDL;
		}
		
		if(same3.length>2){
			total.push(same3);
		}
		return total;
	},
	
	canDisapear(){
		var total = this.findSame();
		if(total.length>0){
			return true;
		}else{
			return false;
		}
	},
	
	getNoDisapearTypes(typenmb){
		var types=[];
		for(var i=0;i<typenmb;i++){
			this.type1 = i;
			if(this.canDisapear()){
				
			}else{
				types.push(i);
			}
		}
		return types;
		
	},
	
	exchange(tmp,callback){
		
		var type11 = this.type1;
		var type12 = tmp.type1;
		
		var type21 = this.type2;
		var type22 = tmp.type2;
		
		var node1 = this.blockNode;
		var node2 = tmp.blockNode;
							
		var p1 = this.mapNode.getPosition();
		var p2 = tmp.mapNode.getPosition();
							
		this.blockNode = node2;
		tmp.blockNode = node1;
		
		this.type1 = type12;
		tmp.type1 = type11;
		
		this.type2 = type22;
		tmp.type2 = type21;
							
		node1.runAction(cc.moveTo(0.2,p2));
		
		
        node2.runAction(cc.sequence(cc.moveTo(0.2,p1), cc.delayTime(0.1),callback));
		
		
	},
	
	/*
	topY(){
		return this.topMp().y;
	},*/
	
	topMp(){
		var up = this,next = up.mpU;
		
		/*
		do{
			if(up.mpU!=null){
				up = up.mpU;
			}else if(up.mpUL!=null){
				up = up.mpUL;
			}else{
				up = up.mpUR;
			}
			
		}while(!up.createFlag&&up!=null);*/
		
		while(next!=null){
			
			up = next;
			next = next.mpU;
			
		}
		
		
		return up;
	},
	
	resetType2(type2){
		this.type2 = type2;
		this.blockNode.getComponent(cc.Sprite).spriteFrame = this.game.blockPointFrameArr[this.type1][this.type2];
	},
	
	resetType(type1,type2){
		this.type1 = type1;
		this.type2 = type2;
		this.blockNode.getComponent(cc.Sprite).spriteFrame = this.game.blockPointFrameArr[this.type1][this.type2];
	},
	
	isboom(){
		return this.mpD==null&&this.mpDR==null&&this.mpDL==null;
	},
	
	isLine(){
		return this.type2==2||this.type2==3||this.type2==4;
	}
});

















